<?php

if(!defined('IN_JANSEN')) {
    exit('Access Denied');
}

class cart {
    /*
    * еƷ,磺
    * $myCart[5][$name]:ƷΪ5
    * $myCart[5][$price]:ƷΪ5ĵ
    * $myCart[5][$count]:ƷΪ5
    * $myCart[5][$cost]:ƷΪ5ĺϼƽ
    */

    var $sortCount;    //Ʒ
    var $totalCost;    //Ʒܽ

    var $myCart;
    var $Id;           //ÿƷID
    var $Name;         //ÿƷ
    var $Price;        //ÿƷļ۸
    var $Cbm;          
    var $Cuft;        
    var $Count;        //ÿƷļ
    var $Cost;         //ÿƷļֵ
    var $inType;       //from product or inventory
	var $fitemid; //FinishѡID


    // 캯
    function cart() {
        $this->sortCount = 0;
        $this->totalCost = 0;
        $this->myCart = array();
        $this->Id = 0;
        $this->Name = '';
        $this->Price = 0;
        $this->Cbm = 0;
        $this->Cuft = 0;
        $this->Count = 0;
		$this->Cost = 0;
		$this->inType = 0;
		$this->fitemid = 0;

        if(!session_is_registered("myCart")) {
            session_register('myCart');			
        }	
        $this->update();
    }

    // privatesessionֵӦ
    function update() {
        $myCart = $_SESSION["myCart"];
        if(false==$myCart ) {
            $this->sortCount = 0;
            $this->totalCost = 0;
            $this->myCart = array();
            $this->Id = 0;
            $this->Name = '';
            $this->Price = 0;
            $this->Cbm = 0;
            $this->Cuft = 0;
            $this->Count = 0;
            $this->Cost = 0;
			$this->inType = 0;
			$this->fitemid = 0;
            
            return false;
        }
        
        //õƷ
        $this->sortCount=count($myCart);
        
        if($this->sortCount>0) {
            //ʼƷĽ
            $totalCost = 0    ;
            foreach($myCart as $key=>$val) {
                //,2λЧ
                foreach($val as $proName=>$proVal) {
                    if($proName !="name") {
                        $val[$proName] = round(eregi_replace(",", "",$proVal),2);
                        $myCart[$key][$proName] = $val[$proName];
                    }
                }

                //ÿƷĽ
                $myCart[$key]["cost"] = round($val["count"]*$val["price"], 2);
                
                //õƷĽ
                $totalCost += $myCart[$key]["cost"];
            }
            
            $this->totalCost = $totalCost;
            $this->myCart = $myCart;
            $_SESSION["myCart"] = $myCart;			
        }
    }

    // ʽΪ
    function formatNum($data) {
        foreach($data as $key=>$val) {
            foreach($val as $sName=>$sValue) {
                if($sName !="name") {
                    $data[$key][$sName] = number_format($sValue, 2);
                }
            }
        }
        return $data;
    }

    // һƷ
    // жǷcartУУcountһƷ
    function addOne($id,$na,$pr,$cbm,$cuft,$fitemid,$tp=0)    {
        //$id = $tp == 0 ? 'product_' . $id : 'inventory_' . $id;
		$id = $tp . '_' . $id . '_'.$fitemid;
        $myCart = $_SESSION["myCart"];
        if (!isset($myCart[$id]['count'])) {
            $myCart[$id]['count'] = 0;
        }
        
        //ùﳵе
        $myCart[$id]["name"]  = $na;
        $myCart[$id]["price"] = $pr;
        $myCart[$id]["cbm"] = $cbm;
		$myCart[$id]["cuft"] = $cuft;
		$myCart[$id]["intype"] = $tp;
		$myCart[$id]["fitemid"] = $fitemid;
		
        ++$myCart[$id]["count"];
        $_SESSION["myCart"] = $myCart;
        
        //һĳԱ
        $this->update();
    }
    
    /**
    * ﳵһƷ,ûУӣѾڣΪdata
    * @param $data  - ҪӵƷ,ʽΪ
    *                 $data[0][id],   $data[0][name],
    *                 $data[0][price],$data[0][count],$date[0][intype]
    * @return boolean
    */
    function addData($data)
    {
        if(count($data > 0)) {
            
            $myCart = $_SESSION["myCart"];
            foreach($data as $val) {
                extract($val);
				$id = $intype . '_' . $id . '_'.$fitemid;
                //ùﳵе
                $myCart[$id]["name"]  = $name;
                $myCart[$id]["price"] = $price;
                $myCart[$id]["cbm"] = $cbm;
                $myCart[$id]["cuft"] = $cuft;
				$myCart[$id]["count"] = $count;
				$myCart[$id]["intype"] = $intype;
				$myCart[$id]["fitemid"] = $fitemid;
            }
            
            $_SESSION["myCart"] = $myCart;
            
            //һĳԱ
            $this->update();
        }
    }
    
    // һƷĵ
    function updatePrice($id, $price) {
        if($price <=0)  return false;
        
        $myCart = $_SESSION["myCart"];
        if($myCart[$id]==true) {
            $myCart[$id]["price"]=$price;
            $_SESSION["myCart"] = $myCart;
            $this->update();
        }
    }
    
    // һƷ1
    function removeOne($id) {
        $count = $this->myCart[$id]["count"];
        if($count>0) {
            $this->modifyCount($id, --$count);
        }
    }

    //ıƷĸ,뵥ۣһĵ
    function modifyCount($id, $ncount, $price=0) {
        if($ncount <= 0) return false;
        $myCart = $_SESSION["myCart"];
        
        //$id = $tp . '_' . $id;
        
        if($myCart[$id]==true) {
            $myCart[$id]["count"]=$ncount;
            
            //д뵥ۣһĵ
            if($price >0 ) $myCart[$id]["price"]=$price;

            $_SESSION["myCart"] = $myCart;
            
            $this->update();
        }

    }

    //һƷ
    function emptyOne($i) {
        $myCart = $_SESSION["myCart"];
        unset($myCart[$i]);
        if(count($myCart)==0) {
            $this->emptyAll();
        } else {
            $_SESSION["myCart"] = $myCart;
            $this->update();
        }
    }

    // еƷ
    function emptyAll() {
		$_SESSION["myCart"] = "";
        $this->update();
		//echo $this->getSortCount();exit;
		
        session_unset($_SESSION["myCart"]);
        session_destroy();
        cookie(session_name(), '', 0);
    }

    // йﳵе
    function getData() {
        if($this->sortCount > 0) 
		{		
            return $this->myCart;			
        }else{
            return array();
        }
    }
    
    //ȡһƷϢarray('id','name','price','count','cost');
    function getOne($i) {
        $data = $this->myCart[$i];
        if(false==$data) return array();

        $data["id"] = $i;
        return $data;
    }

    //ȡܵƷ
    function getSortCount() {
        return $this->sortCount;
    }

    //ȡܵƷֵ
    function getTotalCost() {
        return $this->totalCost;
    }

	//֤idǷѾ
	function isKeyExists($id)
	{
		return array_key_exists($id,$this->myCart);	
	}
	//Finish item
	function modifyKey($oldid,$new_fitemid,$num)
	{
		if(!is_numeric($num)) return;
		
		$aryTmp = explode('_',$oldid);
		array_pop($aryTmp);
		$aryTmp[]=$new_fitemid;
		$newid = implode('_',$aryTmp);
	
		if($this->isKeyExists($newid))
		{
			$this->emptyOne($oldid);
			$data = $this->getOne($newid);			
			$this->modifyCount($newid,$data["count"]+$num,$data["price"]);
		}
		else
		{
			$myCart = $_SESSION["myCart"];			
			$data = $myCart[$oldid];
			unset($myCart[$oldid]);
			if(!$data) return;
			
			$data["count"] = $num;
			$data["fitemid"] = $new_fitemid;
			$myCart[$newid] = $data;			
			
			$_SESSION["myCart"] = $myCart;
						
			$this->update();
		}
	}
}
?>